package com.gitee.zw.irmp.common.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class HostInfoUtil {

    /**
     * 获取当前服务器主机名
     */
    public static String getHostName() {
        String hostname = System.getenv("COMPUTERNAME");
        if (StringUtils.isEmpty(hostname)) {
            hostname = System.getenv("HOSTNAME");
        }
        if (hostname != null && hostname.split("\\.").length > 0) {
            hostname = hostname.split("\\.")[0];
        }
//        String hostname = null;
//        InetAddress addr = null;
//        try {
//            addr = InetAddress.getLocalHost();
//
//            //获取本机ip
//            //String ip=addr.getHostAddress().toString();
//            //获取本机计算机名称
//            hostname=addr.getHostName().toString();
//
//        } catch (UnknownHostException e) {
//            e.printStackTrace();
//        }
        return hostname;
    }


    /**
     * 获取请求IP地址
     */
    public static String getClientIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        ipAddress = request.getHeader("x-forwarded-for");
        if ((ipAddress == null) || (ipAddress.length() == 0)
                || ("unknown".equalsIgnoreCase(ipAddress))) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if ((ipAddress == null) || (ipAddress.length() == 0)
                || ("unknown".equalsIgnoreCase(ipAddress))) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if ((ipAddress == null) || (ipAddress.length() == 0)
                || ("unknown".equalsIgnoreCase(ipAddress))) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1")) {
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                    ipAddress = inet.getHostAddress();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }

            }

        }
        if ((ipAddress != null) && (ipAddress.length() > 15)) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

    public static String getServerIpAddress() throws SocketException, UnknownHostException {
        Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
        String ipAddress = "";
        while (netInterfaces.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
            //网络地址枚举
            Enumeration<InetAddress> addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress ip = (InetAddress) addresses.nextElement();
                if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                    ipAddress = ip.getHostAddress();
                    break;
                }
            }
        }
        if ("".equals(ipAddress)) {
            InetAddress address = InetAddress.getLocalHost();
            ipAddress = address.getHostAddress();
        }
        return ipAddress;
    }
}
